package com.example.rocketmq.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.rocketmq.dao.OrderRepository;
import com.example.rocketmq.dao.ProductRepository;
import com.example.rocketmq.entity.Order;
import com.example.rocketmq.entity.Product;
import com.example.rocketmq.rocketmq.RocketMQProducer;
import com.example.rocketmq.service.OrderService;
import com.example.rocketmq.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Optional;


/**
 * @author kangshuai
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private ProductRepository productRepository;

    @Autowired
    private RocketMQProducer producer;

    @Autowired
    private ProductService productService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Order findOne(Integer orderId) {
        Optional<Order> orderOptional = orderRepository.findById(orderId);
        if (orderOptional.isPresent()) {
            return orderOptional.get();
        }
        return null;
    }

    @Override
    @Transactional
    public Order newOrder(Order order) {
        order.setStatus("ING");
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());

        //减库存
        Optional<Product> optionalProduct = productRepository.findById(order.getProductId());
        Product product = optionalProduct.get();
        product.setStockNum(product.getStockNum() - 1);
        productRepository.save(product);

        //生成订单
        Order savedOrder = orderRepository.save(order);

        //发送订单延迟消息
        producer.sendDelayMsg(JSON.toJSONString(savedOrder), 4);

        return savedOrder;
    }

    @Override
    public String payOrder(Integer orderId) {
        Order order = orderRepository.findById(orderId).get();

        if ("TIMEOUT".equals(order.getStatus())) {
            return "订单已超时";
        }

        if ("PAID".equals(order.getStatus())) {
            return "订单已支付";
        }

        order.setStatus("PAID");
        order.setUpdateTime(new Date());
        orderRepository.save(order);

        return "支付成功";
    }

    @Override
    @Transactional
    public Order timeoutOrder(Integer orderId) {
        Optional<Order> orderOptional = orderRepository.findById(orderId);
        if (orderOptional.isPresent()) {
            Order order = orderRepository.findById(orderId).get();

            if (!"PAID".equals(order.getStatus()) && !"TIMEOUT".equals(order.getStatus())) {
                //恢复库存数量
                redisTemplate.opsForValue().increment("product:stock:" + order.getProductId());
                Product product = productService.findOne(order.getProductId());
                product.setStockNum(product.getStockNum() + 1);
                productService.save(product);

                order.setStatus("TIMEOUT");
                order.setUpdateTime(new Date());
                return orderRepository.save(order);
            }
        }
        return null;
    }
}
